package com.qfedu.web;


import com.qfedu.pojo.*;
import com.qfedu.service.AccountService;
import com.qfedu.service.CardsService;
import com.qfedu.service.CreditCardService;
import com.qfedu.service.ProductService;
import com.qfedu.service.impl.AccountServiceImpl;
import com.qfedu.service.impl.CardsServiceImpl;
import com.qfedu.service.impl.CreditCardServiceImpl;
import com.qfedu.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

@WebServlet("/personalServlet")
public class PersonalServlet extends BaseServlet{
    AccountService accountService = new AccountServiceImpl();
    CardsService cardsService = new CardsServiceImpl();
    CreditCardService creditCardService = new CreditCardServiceImpl();
    ProductService productService = new ProductServiceImpl();
    public void findAllCards(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute("user");
        String idcode = accountService.queryIdcodeByPhone(user.getPhone()).getIdcode();
        List<Cards> mycards =  cardsService.findAllByIdcode(idcode);
        request.getSession().setAttribute("mycards",mycards);
        request.getRequestDispatcher("/pages/personal/mycards.jsp").forward(request,response);
    }

    public void findMyAsset(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute("user");
        String idcode = accountService.queryIdcodeByPhone(user.getPhone()).getIdcode();
        Account account = accountService.queryAccountByIdcode(idcode);
        BigDecimal productMoney = productService.backAllProductsPrice(idcode);
        DecimalFormat decimalFormat = new DecimalFormat("0.00#");
        String myProPrice = decimalFormat.format(productMoney);
        BigDecimal decimal = new BigDecimal(myProPrice);
        List<Cards> mycards =  cardsService.findAllByIdcode(idcode);
        request.getSession().setAttribute("mycards",mycards);
        BigDecimal cardsum = new BigDecimal(0);
        if(mycards!=null){
            for (Cards c:mycards) {
                cardsum=cardsum.add(c.getMoney());
            }
        }
        List<CreditCard> myCredits = creditCardService.findIdcodeCredit(idcode);
        BigDecimal debt = new BigDecimal(0);
        if(myCredits!=null){
            for (CreditCard c:myCredits) {
                debt=debt.add(c.getBalance());
            }
        }
        BigDecimal totalAsset = cardsum.add(account.getMoney()).add(decimal);
        request.getSession().setAttribute("totalAsset",totalAsset);
        request.getSession().setAttribute("debt",debt);
        request.getSession().setAttribute("cardsum",cardsum);
        request.getSession().setAttribute("account",account);
        request.getSession().setAttribute("productMoney",decimal);
        request.getRequestDispatcher("/pages/personal/asset_info.jsp").forward(request,response);
    }

    public void myHome(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute("user");
        String idcode = accountService.queryIdcodeByPhone(user.getPhone()).getIdcode();
        List<Cards> mycards =  cardsService.findAllByIdcode(idcode);
        request.getSession().setAttribute("mycards",mycards);
        BigDecimal cardsum = new BigDecimal(0);
        if(mycards!=null){
            for (Cards c:mycards) {
                cardsum=cardsum.add(c.getMoney());
            }
        }
        List<CreditCard> myCredits = creditCardService.findIdcodeCredit(idcode);
        BigDecimal debt = new BigDecimal(0);
        if(myCredits!=null){
            for (CreditCard c:myCredits) {
                debt=debt.add(c.getBalance());
            }
        }
        Account account = accountService.queryAccountByIdcode(idcode);
        BigDecimal productMoney = productService.backAllProductsPrice(idcode);
        BigDecimal totalAsset = cardsum.add(account.getMoney()).add(productMoney);
        request.getSession().setAttribute("totalAsset",totalAsset);
        request.getSession().setAttribute("debt",debt);
        request.getRequestDispatcher("/pages/personal/myhome.jsp").forward(request,response);

    }

    public void findAllCredit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute("user");
        String idcode = accountService.queryIdcodeByPhone(user.getPhone()).getIdcode();
        List<CreditCard> myCredits = creditCardService.findIdcodeCredit(idcode);
        request.getSession().setAttribute("myCredits",myCredits);
        request.getRequestDispatcher("/pages/personal/mycredits.jsp").forward(request,response);


    }
}
